<template>
  <div class="payment-container">
    <!-- 面包屑导航 -->
    <div class="breadcrumb-section">
      <el-breadcrumb :separator-icon="ArrowRight">
        <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item :to="{ path: '/cart' }">购物车</el-breadcrumb-item>
        <el-breadcrumb-item :to="{ path: '/checkout' }">确认订单</el-breadcrumb-item>
        <el-breadcrumb-item>支付订单</el-breadcrumb-item>
      </el-breadcrumb>
    </div>

    <!-- 页面标题 -->
    <div class="page-header">
      <h1 class="page-title">支付订单</h1>
    </div>

    <div v-if="!loading" class="payment-content">
      <!-- 订单信息 -->
      <div class="payment-section">
        <div class="section-title">
          <el-icon><Wallet /></el-icon>
          <span>订单信息</span>
        </div>
        <div class="section-content">
          <div class="order-info">
            <div class="info-item">
              <span class="info-label">订单编号：</span>
              <span class="info-value">{{ orderId }}</span>
            </div>
            <div class="info-item">
              <span class="info-label">订单金额：</span>
              <span class="info-value amount">¥{{ orderAmount.toFixed(2) }}</span>
            </div>
          </div>
        </div>
      </div>

      <!-- 支付方式 -->
      <div class="payment-section">
        <div class="section-title">
          <el-icon><CreditCard /></el-icon>
          <span>选择支付方式</span>
        </div>
        <div class="section-content">
          <div class="payment-methods">
            <el-radio-group v-model="selectedPaymentMethod" class="payment-radio-group">
              <el-radio-button
                v-for="method in paymentMethods"
                :key="method.id"
                :label="method.id"
                class="payment-method-item"
              >
                <span :class="method.icon"></span>
                <span>{{ method.name }}</span>
              </el-radio-button>
            </el-radio-group>
          </div>
        </div>
      </div>

      <!-- 支付操作 -->
      <div class="payment-section">
        <div class="payment-actions">
          <el-button
            type="primary"
            @click="handlePayNow"
            :loading="processingPayment"
            class="pay-now-btn"
          >
            {{ processingPayment ? '处理中...' : '立即支付' }}
          </el-button>
          <el-button
            type="default"
            @click="cancelPayment"
            :disabled="processingPayment"
            class="cancel-btn"
          >
            取消
          </el-button>
        </div>
      </div>
    </div>

    <!-- 加载状态 -->
    <div v-else class="loading-container">
      <el-loading v-loading="loading" element-loading-text="加载中..." />
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { useUserStore } from '../../store/modules/user'
import { useOrderStore } from '../../store/modules/order'
import { ArrowRight, Wallet, CreditCard } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'

const router = useRouter()
const route = useRoute()
const userStore = useUserStore()
const orderStore = useOrderStore()

// 从路由参数获取订单ID
const orderId = route.params.orderId

// 响应式数据
const loading = ref(true)
const processingPayment = ref(false)
const orderAmount = ref(0)
const selectedPaymentMethod = ref('alipay')

// 支付方式
const paymentMethods = ref([
  {
    id: 'alipay',
    name: '支付宝',
    icon: 'el-icon-alipay'
  },
  {
    id: 'wechat',
    name: '微信支付',
    icon: 'el-icon-wechat'
  },
  {
    id: 'bank',
    name: '银行卡支付',
    icon: 'el-icon-credit-card'
  }
])

// 加载订单信息
const loadOrderInfo = async () => {
  loading.value = true
  try {
    // 检查用户是否登录
    if (!userStore.isLoggedIn) {
      ElMessage.warning('请先登录')
      router.push('/login')
      return
    }

    // 模拟加载订单信息，实际项目中应调用 orderStore.getOrderDetail(orderId)
    // const orderDetail = await orderStore.getOrderDetail(orderId)
    // orderAmount.value = orderDetail.amount

    // 模拟订单金额
    orderAmount.value = 8299 // 这个值应该从实际订单数据中获取
  } catch (error) {
    ElMessage.error('加载订单信息失败：' + error.message)
    console.error('加载订单信息失败：', error)
  } finally {
    loading.value = false
  }
}

// 处理立即支付
const handlePayNow = async () => {
  if (!orderId) {
    ElMessage.warning('订单ID无效')
    return
  }

  processingPayment.value = true
  try {
    // 模拟支付流程，实际项目中应调用 orderStore.processPayment 或相关API
    // await orderStore.processPayment(orderId, selectedPaymentMethod.value)

    // 模拟支付成功
    // 实际项目中应该根据支付结果进行处理
    setTimeout(() => {
      ElMessage.success('支付成功')
      // 跳转到订单详情页面
      router.push(`/order/${orderId}`)
    }, 2000)
  } catch (error) {
    ElMessage.error('支付失败：' + error.message)
    console.error('支付失败：', error)
  } finally {
    processingPayment.value = false
  }
}

// 取消支付
const cancelPayment = () => {
  // 确认取消
  router.push(`/orders`)
}

// 组件挂载时加载订单信息
onMounted(() => {
  loadOrderInfo()
})
</script>

<style scoped>
.payment-container {
  width: 100%;
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
  box-sizing: border-box;
}

/* 面包屑导航 */
.breadcrumb-section {
  margin: 20px 0;
}

/* 页面标题 */
.page-header {
  margin-bottom: 30px;
}

.page-title {
  font-size: 24px;
  color: #333;
  margin: 0;
}

/* 支付内容 */
.payment-content {
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  padding: 0;
}

/* 支付区块 */
.payment-section {
  border-bottom: 1px solid #e8e8e8;
}

.payment-section:last-child {
  border-bottom: none;
}

/* 区块标题 */
.section-title {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 20px;
  background-color: #fafafa;
  font-size: 16px;
  color: #333;
  font-weight: 500;
}

/* 区块内容 */
.section-content {
  padding: 20px;
}

/* 订单信息 */
.order-info {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.info-item {
  display: flex;
  align-items: center;
  gap: 16px;
}

.info-label {
  font-size: 14px;
  color: #666;
  width: 100px;
}

.info-value {
  font-size: 14px;
  color: #333;
  flex: 1;
}

.info-value.amount {
  font-size: 18px;
  color: #ff4d4f;
  font-weight: 500;
}

/* 支付方式 */
.payment-methods {
  padding: 10px 0;
}

.payment-radio-group {
  display: flex;
  gap: 20px;
}

.payment-method-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 20px;
  border-radius: 4px;
  transition: all 0.3s;
}

.payment-method-item:hover {
  background-color: #f5f5f5;
}

/* 支付操作 */
.payment-actions {
  display: flex;
  justify-content: center;
  gap: 20px;
  padding: 30px 20px;
}

.pay-now-btn {
  padding: 12px 40px;
  font-size: 16px;
}

.cancel-btn {
  padding: 12px 40px;
  font-size: 16px;
}

/* 加载状态 */
.loading-container {
  min-height: 400px;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

/* 响应式设计 */
@media (max-width: 768px) {
  .payment-container {
    padding: 0 10px;
  }

  .payment-radio-group {
    flex-direction: column;
    gap: 10px;
  }

  .payment-method-item {
    width: 100%;
  }

  .payment-actions {
    flex-direction: column;
    gap: 15px;
  }

  .pay-now-btn,
  .cancel-btn {
    width: 100%;
  }
}
</style>